﻿// DbProviderLib
// (c)2007 Mark Bulmahn. mBulmahn@web.de
// License: GPL 2.0
// Project: TestCaseGenerator. http://www.codeplex.com/TestCaseGenerator

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;

namespace DbProviderLib
{
    /// <summary>
    /// A basic Provider for Sql Compact Edition Databases. These are neat for some q&d
    /// tools fiddling with data.
    /// </summary>
    public class SqlCeProvider : DbProviderBase, IDbProvider
    {
        
        SqlCeConnection conn = null;
        public IDbConnection Connection
        {
            get
            {
                return conn;
            }
            set
            {
                conn = (SqlCeConnection)value;
            }
        }

        public SqlCeProvider(string connStr)
        {
            this.conn = new SqlCeConnection(connStr);
            this.conn.Open();
        }

        public IDbCommand GetCommand(string cmdText)
        {
            SqlCeCommand cmd = new SqlCeCommand(cmdText, this.conn);
            return cmd;
        }

        public IDbDataAdapter GetAdapter(string cmdText)
        {
            SqlCeDataAdapter adapter = new SqlCeDataAdapter(cmdText, this.conn);
            SqlCeCommandBuilder bldr = new SqlCeCommandBuilder(adapter);
            return adapter;
        }

        public IDataReader GetReader(string cmdText)
        {
            SqlCeCommand cmd = new SqlCeCommand(cmdText, this.conn);
            SqlCeDataReader dr = cmd.ExecuteReader();
            return dr;
        }

        public static bool CreateSqlCeDatabase(string fileName)
        {
            bool result = false;

            SqlCeEngine engine = new SqlCeEngine("Data Source='" + fileName + "'");
            engine.CreateDatabase();

            return result;
        }
    }
}
